<template>
  <q-dialog class="my-dialog-form" v-model="isFlagShow">
    <q-card class="my-card q-mx-lg">
      <q-toolbar class="row items-center my-form-header">
        <div>
          {{dlgTitle}}
        </div>
        <q-space />
        <q-btn flat round dense icon="mdi-close" @click="close"/>
      </q-toolbar>
      <q-separator />
      <q-card-setion class="q-mx-sm">
        <q-scroll-area class="my-form-container">
          <table class="my_info_table">
            <tr class="my_info_row_height">
              <td class="my_info_label"><span style="color: red">**</span>项目名称：</td>
              <td class="my_info_value">
                <q-select
                  dense
                  outlined
                  options-dense
                  :options="projectArr"
                  v-model="param.project"
                  option-value="label"
                  emit-value
                  options-selected-class="teal"
                  @blur="v$.project.$touch()"
                  :error="v$.project.$error"
                  error-message="请选择项目名称！"
                />
              </td>
            </tr>
            <tr class="my_info_row_height">
              <td class="my_info_label"><span style="color: red">**</span>前端服务器：</td>
              <td class="my_info_value">
                <q-input
                  outlined
                  dense
                  v-model="param.userServerAddr"
                  @blur="v$.userServerAddr.$touch()"
                  :error="v$.userServerAddr.$error"
                  error-message="请输入不少于2个字符！"
                />
              </td>
            </tr>
            <tr class="my_info_row_height">
              <td class="my_info_label"><span style="color: red">**</span>后台服务器：</td>
              <td class="my_info_value">
                <q-input
                  outlined
                  dense
                  v-model="param.adminServerAddr"
                  @blur="v$.adminServerAddr.$touch()"
                  :error="v$.adminServerAddr.$error"
                  error-message="请输入不少于2个字符！"
                />
              </td>
            </tr>

            <tr class="my_info_row_height">
              <td class="my_info_label"><span style="color: red">**</span>组态服务器：</td>
              <td class="my_info_value">
                <q-input
                  outlined
                  dense
                  v-model="param.scaServerAddr"
                  @blur="v$.scaServerAddr.$touch()"
                  :error="v$.scaServerAddr.$error"
                  error-message="请输入不少于2个字符！"
                />
              </td>
            </tr>
            <tr class="my_info_row_height">
              <td class="my_info_label"><span style="color: red">**</span>柔性服务器：</td>
              <td class="my_info_value">
                <q-input
                  outlined
                  dense
                  v-model="param.flexServerAddr"
                  @blur="v$.flexServerAddr.$touch()"
                  :error="v$.flexServerAddr.$error"
                  error-message="请输入不少于2个字符！"
                />
              </td>
            </tr>
            <tr class="my_info_row_height">
              <td class="my_info_label"><span style="color: red">**</span>IO服务器配置文件：</td>
              <td class="my_info_value">
                <q-input
                  outlined
                  dense
                  v-model="param.ioServerFilePath"
                  @blur="v$.ioServerFilePath.$touch()"
                  :error="v$.ioServerFilePath.$error"
                  error-message="请输入不少于2个字符！"
                />
              </td>
            </tr>
          </table>
        </q-scroll-area>
      </q-card-setion>
      <q-separator />
      <div class="q-gutter-sm my-form-footer">
        <q-btn
          unelevated
          dense
          label="提交"
          type="submit"
          color="teal-3"
          @click="onSubmit"
          style="width: 80px"
        />
        <q-btn
          unelevated
          dense
          label="重置"
          type="reset"
          color="orange-3"
          @click="onReset"
          style="width: 80px"
        />
      </div>
    </q-card>
  </q-dialog>
</template>

<script lang="ts">
import { defineComponent, onMounted, ref, toRef, toRefs, PropType, watch } from 'vue';
import { isObjectEmpty } from '../../../util/utils';
import { required, minLength } from '@vuelidate/validators'
import { useVuelidate } from '@vuelidate/core';
import { cataItem } from '../../../hook/common/useCatalogType';
import { useCatalogTool } from 'src/hook/common/useCatalogTool';

import { ApplicationItem, ApplicationItemVO } from './ApplicationModel';


export default defineComponent({
  cname: 'ApplicationDlg',
  props: {
    itemData: {
      type: Object as PropType<ApplicationItem>,
      required: true
    },
    projectArr: {
      type: Array as PropType<cataItem[]>,
      required: true
    }
  },
  emits: ['submit-application-item', 'cancel-application-item'],
  setup (props, context) {
    //数据解构
    const {
      itemData,
      projectArr
    } = toRefs(props);

    //*******************************
    //      目录操作
    //*******************************
    const {
      findCatalogLabel,
      findCatalogId
    } = useCatalogTool();

    //*******************************
    //      表单项
    //*******************************
    //窗口显示
    let isFlagShow = ref(false);

    //窗口标题
    let dlgTitle = ref('');

    //界面数据
    let param = ref<ApplicationItemVO>({
      id: 0,
      project: '',
      userServerAddr: '',
      adminServerAddr: '',
      scaServerAddr: '',
      flexServerAddr: '',
      ioServerFilePath: ''
    });

    //关闭窗口
    const close = () => {
      isFlagShow.value = false;
      context.emit('cancel-application-item');
    }

    //显示窗口
    const show = (text: string) => {
      isFlagShow.value = true;
      dlgTitle.value = text;
    }

    //提交窗口
    const onSubmit = () => {
      v$.value.$validate();
      if (v$.value.$error) {
        alert('输入参数校验错误!');
      } else {
        isFlagShow.value = false;
        let item: ApplicationItem = {
          id: param.value.id,
          projectId: findCatalogId(projectArr.value, param.value.project),
          userServerAddr: param.value.userServerAddr,
          adminServerAddr: param.value.adminServerAddr,
          scaServerAddr: param.value.scaServerAddr,
          flexServerAddr: param.value.flexServerAddr,
          ioServerFilePath: param.value.ioServerFilePath,
          createTime: '',
          createUserId: 0,
          updateTime: '',
          updateUserId: 0
        };

        //发送消息
        context.emit('submit-application-item', item);
      }
    }

    //重置
    const onReset = () => {
      if (param.value) {
        param.value.project = '';
        param.value.userServerAddr = '';
        param.value.adminServerAddr = '';
        param.value.scaServerAddr = '';
        param.value.flexServerAddr = '';
        param.value.ioServerFilePath = '';
      }

      //表单验证初始化
      v$.value.$reset();
    }

    //*******************************
    //      表单校验
    //*******************************
    //配置校验规则
    const rules = {
      id: {},
      project: { required },
      userServerAddr: { required },
      adminServerAddr: { required },
      scaServerAddr: { required },
      flexServerAddr: { required },
      ioServerFilePath: { required }
    }

    //使用vuelidate，建立变量对应的校验规则
    const v$ = useVuelidate(rules, {
      id: toRef(param.value, 'id'),
      project: toRef(param.value, 'project'),
      userServerAddr: toRef(param.value, 'userServerAddr'),
      adminServerAddr: toRef(param.value, 'adminServerAddr'),
      scaServerAddr: toRef(param.value, 'scaServerAddr'),
      flexServerAddr: toRef(param.value, 'flexServerAddr'),
      ioServerFilePath: toRef(param.value, 'ioServerFilePath')
    });


    //*******************************
    //      启动模块
    //*******************************
    //初始化
    const init = () => {
      if (!isObjectEmpty(itemData.value)) {
        param.value.id = itemData.value.id;
        param.value.project = findCatalogLabel(projectArr.value, itemData.value.projectId);
        param.value.userServerAddr = itemData.value.userServerAddr;
        param.value.adminServerAddr = itemData.value.adminServerAddr;
        param.value.scaServerAddr = itemData.value.scaServerAddr;
        param.value.flexServerAddr = itemData.value.flexServerAddr;
        param.value.ioServerFilePath = itemData.value.ioServerFilePath;
      }

      //表单验证初始化
      v$.value.$reset();
    }

    //监听对象的所有属性
    watch(() => itemData, () => {
      init();
    }, { deep: true });

    //启动挂载组件
    onMounted(() => {
      init();
    })

    return {
      itemData,
      projectArr,

      isFlagShow,
      dlgTitle,
      param,
      init,
      close,
      show,
      onSubmit,
      onReset,
      v$
    }
  }
})
</script>

<style lang="scss" scoped>
.my-card {
  alignment: center;
  width: 650px;
  max-width: 800px;

  .my-form-container {
    text-align: center;
    padding: 5px 30px 5px 30px;
    height: 420px;

    .my_info_table {
      .my_info_row_height {
        line-height: 60px;
      }
      .my_info_label {
        text-align: right;
        width: 150px;
      }

      .my_info_value {
        text-align: left;
        width: 400px;
      }
    }

    .my-select-box {
      max-width: 400px;
    }
  }
}

</style>
